package org.ipower.examsystem.service.impl;

import java.util.List;
import org.ipower.examsystem.dao.TeachingSectionsDao;
import org.ipower.examsystem.domain.DataField;
import org.ipower.examsystem.domain.TeachingSections;
import org.ipower.examsystem.service.TeachingSectionsService;
import org.ipower.platform.domain.Pager;
import org.ipower.platform.domain.QueryCondition;

/**
 * 教材章节服务实现类。
 * @author jeasonyoung
 */
public class TeachingSectionsServiceImpl implements TeachingSectionsService {
    private static final long serialVersionUID = 1L;
    private TeachingSectionsDao dao = null;
   
    @Override
    public void setTeachingSectionsDao(TeachingSectionsDao dao) {
        this.dao = dao;
    }
   
    @Override
    public Pager<TeachingSections> findAll(String ascProperty, boolean isAsc, int start, int limit, List<QueryCondition> entites) {
        return this.dao.findAll(ascProperty, isAsc, start, limit, entites);
    }
   
    @Override
    public void saveOrUpdate(TeachingSections data) {
        if(data != null && data.getSectionID() != null && !data.getSectionID().isEmpty()){
            if(this.dao.load(data.getSectionID()) == null){
                this.dao.save(data);
            }else{
                this.dao.update(data);
            }
        }
    }
    
    @Override
    public List<DataField> NotSelfSections(String sectionID) {
        return this.dao.NotSelfSections(sectionID);
    }
   
    @Override
    public List<TeachingSections> bindAllData(String catalogID) {
        return this.dao.bindAllData(catalogID);
    }

    @Override
    public void delete(String sectionID) {
        if(sectionID != null && !sectionID.isEmpty()){
            List<TeachingSections> list = this.dao.loadAll("ParentSectionID", sectionID);
            if(list != null &&list.size() > 0){
                for(int i = 0; i < list.size(); i++){
                    TeachingSections ts = list.get(i);
                    if(ts != null){
                        this.delete(ts.getSectionID());
                    }
                }
            }
            this.dao.delete(sectionID);
        }
    }
}